package com.ss.android.newmedia.e;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;

import com.bytedance.article.common.c.TTUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.util.AppUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public final class InstalledAppTracker2 {
    private static InstalledAppTracker2 m = null;

    private final HandlerThread mHandlerThread;
    private final Handler s;
    Context a;
    long b;
    long c;
    long d;
    long e;
    String f;
    String g;
    List h;
    List i;
    boolean j;
    boolean k;
    boolean l;
    private volatile long n;
    private volatile long o;
    private BaseAppData p;
    private String q;

    private InstalledAppTracker2(Context arg3) {

        this.a = arg3;
        this.mHandlerThread = new HandlerThread("InstalledAppTracker2");
        this.mHandlerThread.start();
        this.s = new Handler(this.mHandlerThread.getLooper());
    }

    public static void a(Context arg10) {
        long v8 = 21600000;
        long v6 = 7200000;
        long v4 = 1000;
        if (arg10 != null) {
            InstalledAppTracker2 v1 = c(arg10);
            try {
                if (v1.p == null) {
                    v1.p = BaseAppData.inst();
                }

                String v0_1 = AppLog.deviceId();
                if (TextUtils.isEmpty(v0_1)) {
                    return;
                }

                v1.q = v0_1;
                if (!NetworkUtils.isNetworkAvailable(v1.a)) {
                    return;
                }

                synchronized (v1) {
                    if (v1.l) {
                        return;
                    }

                    v1.n = v1.p.mSendInstallAppsInterval * v4;
                    if (v1.n < v8) {
                        v1.n = 21600000;
                    }

                    v1.o = v1.p.mSendRecentAppsInterval * v4;
                    if (v1.o < v6) {
                        v1.o = 7200000;
                    }

                    if (!v1.a() && !v1.b()) {
                        return;
                    }
                    v1.l = true;
                }

                v1.s.post(new a(v1, (byte) 0));
            } catch (Exception v0) {
                label_32:
                Logger.w("InstalledAppTracker2", "trySync exception: " + v0);
            }
        }
    }

    public static String getRecentApps(Context arg6) {
        String v2;
        Object v0_3;
        JSONArray v3;
        String v0_2;
        String v1 = null;
        try {
            List v0_1 = ((ActivityManager) arg6.getSystemService(Context.ACTIVITY_SERVICE)).getRecentTasks(30, 2);
            if (v0_1 == null || (v0_1.isEmpty())) {
                v0_2 = v1;
                return v0_2;
            }

            v3 = new JSONArray();
            Iterator v4 = v0_1.iterator();
            while (true) {
                label_14:
                if (!v4.hasNext()) {
                    if (v3.length() <= 0) {
                        return v1;
                    }
                    return v3.toString();
                }
                v0_3 = v4.next();
                if (((ActivityManager.RecentTaskInfo) v0_3).origActivity != null) {
                    v2 = ((ActivityManager.RecentTaskInfo) v0_3).origActivity.getPackageName();
                } else {
                    break;
                }
                if (!android.support.design.a.isTextEmpty(v2)) {
                    v0_2 = v2;
                } else if (((ActivityManager.RecentTaskInfo) v0_3).baseIntent != null) {
                    ComponentName v0_4 = ((ActivityManager.RecentTaskInfo) v0_3).baseIntent.getComponent();
                    if (v0_4 != null) {
                        v0_2 = v0_4.getPackageName();
                    } else {
                        v0_2 = v2;
                    }
                } else {
                    v0_2 = v2;
                }
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    continue;
                }
                v3.put(v0_2);
                continue;
            }
        } catch (Throwable v0) {
            Logger.v("InstalledAppTracker2", "getRecentApps exception: " + v0);
        }
        return v1;
    }

    private static synchronized InstalledAppTracker2 c(Context arg3) {
        InstalledAppTracker2 v0_1;
        if (m == null && arg3 != null) {
            m = new InstalledAppTracker2(arg3.getApplicationContext());
        }

        v0_1 = m;
        return v0_1;
    }

    final boolean a() {
        boolean v0 = System.currentTimeMillis() - this.b > this.n ? true : false;
        return v0;
    }

    final void a(boolean arg11, boolean arg12) {
        String v5_1;
        byte[] v4_2;
        String v0 = "http://ib.snssdk.com/service/1/z_app_stats/";
        byte[] v3 = null;
        try {
            StringBuilder v4_1 = new StringBuilder(v0);
            AppUtil.a(v4_1);
            JSONObject v5 = new JSONObject();
            v5.put("device_id", this.q);
            if (arg11) {
                v4_1.append("&_apps=1");
                v5.put("apps", new JSONArray(this.h));
            }

            if (arg12) {
                v4_1.append("&_recent=1");
                v5.put("recent_apps", new JSONArray(this.i));
            }

            AppLog.j();
            if (this.e == 0) {
                this.e = System.currentTimeMillis();
            }

            v4_1.append("&time_first_send_install_app=").append(this.e);
            v5.put("time_first_send_install_app", this.e);
            v0 = v4_1.toString();
            v3 = v5.toString().getBytes("UTF-8");
            v4_2 = v3;
            v5_1 = v0;
        } catch (Exception v4) {
            v4_2 = v3;
            v5_1 = v0;
        }

        if (v4_2 != null && (NetworkUtils.isNetworkAvailable(this.a))) {
            int v3_1 = 0;
            while (v3_1 < 2) {
                int v0_1 = 8192;
                try {
                    v0 = NetworkUtils.executePost(v0_1, v5_1, v4_2, NetworkUtils.CompressType.GZIP, "application/json; charset=utf-8");
                    if (android.support.design.a.isTextEmpty(v0)) {
                        return;
                    }

                    if (!"success".equals(new JSONObject(v0).getString("message"))) {
                        Logger.w("InstalledAppTracker2", "send app list error: " + v0);
                        return;
                    }

                    long v6 = System.currentTimeMillis();
                    if (arg11) {
                        this.b = v6;
                        this.f = this.g;
                        this.k = true;
                    }

                    if (!arg12) {
                        return;
                    }

                    this.c = v6;
                    this.k = true;
                    return;
                } catch (Throwable v0_2) {
                    switch (TTUtils.getErrorCode(this.a, v0_2)) {
                        case 13:
                        case 14:
                        case 15: {
                            v0_1 = 1;
                            break;
                        }
                        default: {
                            v0_1 = 0;
                            break;
                        }
                    }

                    if (v0_1 == 0) {
                        return;
                    }

                    ++v3_1;
                    continue;
                }
            }
        }
    }

    final boolean b() {
        return System.currentTimeMillis() - this.c > this.o;
    }

    final SharedPreferences c() {
        SharedPreferences v0 = this.a.getSharedPreferences("app_track", 0);
        if (1 != v0.getInt("version", -1)) {
            SharedPreferences.Editor v1 = v0.edit();
            v1.clear();
            v1.putInt("version", 1);
            SharedPreferenceCompat.commit(v1);
        }

        return v0;
    }

    static final class a implements Runnable {
        private final InstalledAppTracker2 a;

        a(InstalledAppTracker2 arg1, byte arg2) {
            this(arg1);
        }

        private a(InstalledAppTracker2 arg1) {
            this.a = arg1;
        }

        public final void run() {
            String v4_1;
            String v0_5;
            Object v0_4;
            int v0_2;
            boolean v6;
            boolean v3_1;
            InstalledAppTracker2 v5;
            InstalledAppTracker2 v0_1;
            boolean v2 = false;
            try {
                if (!this.a.j) {
                    v0_1 = this.a;
                    SharedPreferences v3 = v0_1.c();
                    v0_1.b = v3.getLong("time_last_send_install_app", 0);
                    v0_1.c = v3.getLong("time_last_send_recent_app", 0);
                    v0_1.d = v3.getLong("time_last_collect_app", 0);
                    v0_1.f = v3.getString("tag_last_install_app", "");
                    v0_1.e = v3.getLong("time_first_send_install_app", 0);
                    this.a.j = true;
                }

                v5 = this.a;
                v3_1 = v5.a();
                v6 = v5.b();
                if (!v3_1 && !v6) {
                } else {
                    v0_2 = System.currentTimeMillis() - v5.d > 1200000 ? 1 : 0;
                    if (v0_2 != 0) {
                        v5.h = new ArrayList();
                        List v0_3 = v5.a.getPackageManager().getInstalledApplications(0);
                        if (v0_3 != null) {
                            Iterator v4 = v0_3.iterator();
                            while (v4.hasNext()) {
                                v0_4 = v4.next();
                                if (v0_4 == null) {
                                    continue;
                                }

                                v0_5 = ((ApplicationInfo) v0_4).packageName;
                                if (android.support.design.a.isTextEmpty(v0_5)) {
                                    continue;
                                }

                                v5.h.add(v0_5);
                            }
                        }

                        v0_3 = v5.h;
                        Collections.sort(v0_3);
                        v0_5 = com.bytedance.common.utility.aa.md5(Arrays.deepToString(v0_3.toArray()));
                        if (v0_5 == null) {
                            v0_5 = "";
                        }

                        v5.g = v0_5;
                        v5.i = new ArrayList();
                        v0_3 = ((ActivityManager) v5.a.getSystemService(Context.ACTIVITY_SERVICE)).getRecentTasks(30, 1);
                        if (v0_3 != null) {
                            Iterator v7 = v0_3.iterator();
                            while (true) {
                                label_115:
                                if (v7.hasNext()) {
                                    v0_4 = v7.next();
                                    if (v0_4 == null) {
                                        continue;
                                    }
                                    v4_1 = null;
                                    if (((ActivityManager.RecentTaskInfo) v0_4).origActivity != null) {
                                        v4_1 = ((ActivityManager.RecentTaskInfo) v0_4).origActivity.getPackageName();
                                    }

                                    if (!android.support.design.a.isTextEmpty(v4_1)) {
                                        v0_5 = v4_1;
                                    } else if (((ActivityManager.RecentTaskInfo) v0_4).baseIntent != null) {
                                        ComponentName v0_6 = ((ActivityManager.RecentTaskInfo) v0_4).baseIntent.getComponent();
                                        if (v0_6 != null) {
                                            v0_5 = v0_6.getPackageName();
                                        } else {
                                            v0_5 = v4_1;
                                        }
                                    } else {
                                        v0_5 = v4_1;
                                    }

                                    if (android.support.design.a.isTextEmpty(v0_5)) {
                                        continue;
                                    }

                                    v5.i.add(v0_5);
                                    continue;
                                }
                                break;
                            }
                        }
                        v5.d = System.currentTimeMillis();
                        if (v5.f != null && (v5.f.equals(v5.g))) {
                            v0_2 = 1;
                        } else {
                            v0_2 = 0;
                        }
                    } else {
                        v0_2 = 0;
                    }
                    if (v5.h == null) {
//                    goto label_35;
                    } else if (v5.i == null) {
//                    goto label_35;
                    } else {
                        if (!v3_1) {
                            v2 = v3_1;
                        } else if (v0_2 != 0) {
                            v5.b = System.currentTimeMillis();
                            v5.k = true;
                        } else {
                            v2 = v3_1;
                        }
                        if (!v2 && !v6) {
//                    goto label_35;
                        } else {
                            v5.a(v2, v6);
                        }
                    }
                }

                if (!this.a.k) {
                    //label_60:
                    InstalledAppTracker2 v1_1 = this.a;
                    synchronized (v1_1) {
                        v1_1.l = false;
                        return;
                    }
                }
                v0_1 = this.a;
                SharedPreferences.Editor v1 = v0_1.c().edit();
                v1.putLong("time_last_send_install_app", v0_1.b);
                v1.putLong("time_last_send_recent_app", v0_1.c);
                v1.putLong("time_last_collect_app", v0_1.d);
                v1.putLong("time_first_send_install_app", v0_1.e);
                v1.putString("tag_last_install_app", v0_1.f);
                SharedPreferenceCompat.commit(v1);
                this.a.k = false;

            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }
}

